package net.myspring.common.utils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Arrays;
import java.util.List;

/**
 * Created by liuj on 2016/11/10.
 */
public class DateUtils {

    public static LocalDate parseLocalDate(String localDateStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Const.DateFormat.DATE.getValue());
        LocalDate localDate = LocalDate.parse(localDateStr, formatter);
        return localDate;
    }

    public static LocalDateTime parseLocalDateTime(String localDateTimeStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Const.DateFormat.DATE_TIME.getValue());
        LocalDateTime localDateTime = LocalDateTime.parse(localDateTimeStr, formatter);
        return localDateTime;
    }

    public static String formatLocalDate(LocalDate localDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Const.DateFormat.DATE.getValue());
        return localDate.format(formatter);
    }

    public static String formatLocalDateTime(LocalDateTime localDateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Const.DateFormat.DATE_TIME.getValue());
        return localDateTime.format(formatter);
    }

    public static LocalDate getFirstDayOfLastMonth(LocalDate date){
        LocalDate initial = date.minusMonths(1);
        return initial.withDayOfMonth(1);
    }

    public static LocalDate getLastDayOfLastMonth(LocalDate date){
        LocalDate initial = date.minusMonths(1);
        return initial.withDayOfMonth(initial.lengthOfMonth());
    }

    public static LocalDate getFirstDayOfLastWeek(LocalDate date){
        LocalDate lastMonday = date.with(DayOfWeek.MONDAY).minusWeeks(1) ;
        return lastMonday;
    }

    public static LocalDate getLastDayOfLastWeek(LocalDate date){
        LocalDate lastMonday = date.with(DayOfWeek.SUNDAY).minusWeeks(1);
        return lastMonday;
    }

}
